package com.example.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Order1;
import com.example.entity.UserAction;
import com.example.service.LogService;
import com.example.service.Order1Service;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-27
 */
@RestController
@RequestMapping("//order1")
public class Order1Controller {

    @Resource
    private Order1Service order1Service;
    @Resource
    private LogService logService;
    @Resource
    private HttpServletRequest request;

    @PostMapping
    public Result<Order1> save(Order1 order1) {
        order1.setTime(DateUtil.formatDateTime(new Date()));
        order1.setState("未支付");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String orderNumber = sdf.format(new Date());
        order1.setOrderid(orderNumber);
        logService.log(StrUtil.format("新增订单：{} ",order1.getUsername()));
        order1Service.save(order1);
        return Result.success(order1);
    }


    @GetMapping("/{id}")
    public Result<Order1> findById(@PathVariable Long id) {
        return Result.success(order1Service.getById(id));
    }

    @GetMapping
    public Result<List<Order1>> findAll() {
        return Result.success(order1Service.list());
    }
    @GetMapping("/page")
    public Result<IPage<Order1>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(order1Service.page(new Page<>(pageNum, pageSize), Wrappers.<Order1>lambdaQuery().like(Order1::getUsername, name)));
    }
//    @GetMapping("/{username}")
//    public Result<List<Order1>> findById(@PathVariable String username) {
//        return Result.success(order1Service.getlist(username));
//    }

}

